﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DrWileyO_REDACTED_Adventure
{
    class Platform
    {
        Texture2D cap;
        Texture2D middlePieces;
        Vector2 position;
        int middleNumber;
        bool isVertical;

        public bool isFalling;
        public bool fallStart = false;
        bool fallen = false;
        int count = 1000;
        SpriteFont sFont;

        public Rectangle hitBox;

        Texture2D debugHitBoxImage;

        public Platform(Texture2D cap, Texture2D middle, int middleNum, Vector2 pos, bool isVertical, Texture2D debug, bool falling, SpriteFont font)
        {
            this.isVertical = isVertical;
            
            this.cap = cap;
            middlePieces = middle;

            isFalling = falling;
            sFont = font;

            debugHitBoxImage = debug; //debug purposes

            middleNumber = middleNum;
            position = pos;
            if (!isVertical)
                hitBox = new Rectangle((int)position.X, (int)position.Y, this.cap.Width + middlePieces.Width * middleNumber + this.cap.Width, this.cap.Height);
            else
                hitBox = new Rectangle((int)position.X, (int)position.Y, this.cap.Width, this.cap.Height + middlePieces.Height * middleNumber + this.cap.Height);

            if (middleNum == -1)
            {
                if (!isVertical)
                {
                    hitBox = new Rectangle((int)position.X, (int)position.Y, this.cap.Width, this.cap.Height);
                }
                else
                {
                    hitBox = new Rectangle((int)position.X, (int)position.Y, this.cap.Width, this.cap.Height);
                }
            }
        }

        public void Update(GameTime gTime)
        {
            if (fallStart && !fallen)
            {
                count -= 10;
            }
            if (count <= 0)
            {
                fallen = true;
                hitBox = Rectangle.Empty;
            }
        }

        public void Draw(GameTime gTime, SpriteBatch sBatch, float alpha)
        {
            if (!fallen)
            {
                if (middleNumber == -1)
                {
                    sBatch.Draw(cap, position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, .5f);
                }
                else
                {
                    if (!isVertical)
                    {
                        sBatch.Draw(cap, position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, .5f);

                        int acc = cap.Width;
                        for (int i = 0; i < middleNumber; i++)
                        {
                            sBatch.Draw(middlePieces, new Vector2(position.X + acc, position.Y), null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, .5f);
                            acc += middlePieces.Width;
                        }
                        Vector2 endPos = new Vector2(position.X + acc, position.Y);
                        sBatch.Draw(cap, endPos, null, Color.White, 0.0f, new Vector2(0, 0), 1.0f, SpriteEffects.FlipHorizontally, .5f);
                    }
                    else
                    {
                        sBatch.Draw(cap, position, null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, .5f);

                        int acc = cap.Height;
                        for (int i = 0; i < middleNumber; i++)
                        {
                            sBatch.Draw(middlePieces, new Vector2(position.X, position.Y + acc), null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, .5f);
                            acc += middlePieces.Height;
                        }
                        Vector2 endPos = new Vector2(position.X, position.Y + acc);
                        sBatch.Draw(cap, endPos, null, Color.White, 0.0f, new Vector2(0, 0), 1.0f, SpriteEffects.FlipHorizontally & SpriteEffects.FlipVertically, .5f);
                    }
                }
                if (isFalling && !fallen)
                {
                    sBatch.DrawString(sFont, count / 100 + "", new Vector2(hitBox.Center.X, hitBox.Center.Y - hitBox.Height * .25f), Color.Red, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, .6f);
                }
            }
           // sBatch.Draw(debugHitBoxImage, hitBox, Color.White);
        }

        public static ArrayList GetLevel(Texture2D cap, Texture2D mid, Texture2D capv, Texture2D midv, Texture2D debug, SpriteFont sFont)
        {
            ////// Generates the level HURR DURR
            ArrayList level = new ArrayList();

            //start here
            level.Add(new Platform(cap, mid, 4, new Vector2(-192, 448), false, debug, false, sFont));

            //walls
            level.Add(new Platform(cap, mid, 37, new Vector2(0, -64), false, debug, false, sFont));
            level.Add(new Platform(capv, midv, 3, new Vector2(-64, 0), true, debug, false, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(0, 320), false, debug, false, sFont));
            level.Add(new Platform(capv, midv, 8, new Vector2(2496, 0), true, debug, false, sFont));
            level.Add(new Platform(capv, midv, 5, new Vector2(-200, 0), true, debug, false, sFont));

            //platforms
            level.Add(new Platform(cap, mid, 7, new Vector2(0, 40), false, debug, false, sFont));
            level.Add(new Platform(cap, mid, 1, new Vector2(128, 192), false, debug, false, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(192, 320), false, debug, false, sFont));
            level.Add(new Platform(capv, midv, 1, new Vector2(320, 320), true, debug, false, sFont));
            level.Add(new Platform(cap, mid, 2, new Vector2(320, 512), false, debug, false, sFont));
            level.Add(new Platform(cap, mid, 1, new Vector2(400, 320), false, debug, false, sFont));
            level.Add(new Platform(capv, midv, 1, new Vector2(576, 300), true, debug, false, sFont));
            level.Add(new Platform(cap, mid, 2, new Vector2(576, 512), false, debug, false, sFont));

            level.Add(new Platform(cap, mid, 3, new Vector2(704, 192), false, debug, false, sFont));
            
            level.Add(new Platform(cap, mid, -1, new Vector2(960, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1088, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1216, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1344, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1472, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1600, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1600, 448), false, debug, true, sFont));

            level.Add(new Platform(cap, mid, 2, new Vector2(1600, 320), false, debug, true, sFont));

            level.Add(new Platform(capv, midv, 4, new Vector2(1024, 0), true, debug, false, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1088, 192), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1088, 320), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1088, 448), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, 3, new Vector2(1216, 256), false, debug, false, sFont));
            level.Add(new Platform(cap, mid, 3, new Vector2(1216, 384), false, debug, false, sFont));
            level.Add(new Platform(capv, midv, 4, new Vector2(1792, 0), true, debug, false, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(1472, 576), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, 3, new Vector2(1728, 512), false, debug, false, sFont));

            level.Add(new Platform(cap, mid, -1, new Vector2(2048, 128), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(2048, 256), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(2048, 384), false, debug, true, sFont));

            level.Add(new Platform(cap, mid, -1, new Vector2(2248, 64), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(2248, 192), false, debug, true, sFont));
            level.Add(new Platform(cap, mid, -1, new Vector2(2248, 320), false, debug, true, sFont));

            //level.Add(new Platform(capv, midv, 7, new Vector2(2368, 64), true, debug, false, sFont));      

            return level;
        }
    }
}
